'use strict';

const Service = require('egg').Service;

class NewsService extends Service {

  async list(page, limit) {
    const { app } = this;
    const index = (page - 1) * limit;
    // const list = await app.mysql.query('SELECT * FROM news limit ' + (page - 1) * limit + ',' + limit);
    const list = await app.mysql.query(`SELECT * FROM news limit ${index},${limit}`);
    return list;
  }

  async count() {
    const { app } = this;
    const [{ num }] = await app.mysql.query('SELECT count(*) as num FROM news');
    return num;
  }

  async getNewsById(id) {
    const { app } = this;
    const result = await app.mysql.query(`select * from news where id = ${id}`);
    return result;
  }

  async delNewsById(id) {
    const { app } = this;
    const result = await app.mysql.query(`delete from news where id = ${id}`);
    return result;
  }

  async save(id, content, content1, content2) {
    const { app } = this;
    const result = await app.mysql.query(`update news set content = '${content}', content1 = '${content1}', 
                                          content2 = '${content2}' where id = ${id}`);
    return result;
  }
}

module.exports = NewsService;
